﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OptusMobile.Web.ServerComponents.Common;
using OptusMobile.Web.ServerComponents.DAL;

namespace OptusMobile.Web.ServerComponents.BAL.CustomerManagement
{
    public class CustomerManager : EFManagerBase
    {
        public CustomerContract GetCustomerContractInformation(string mobileNumber)
        {
            CustomerContract customerContract = new CustomerContract();
            try
            {
                customerContract = this.ObjectContext.CustomerContractSet.Include("HandsetContract").
                    FirstOrDefault<CustomerContract>(it => it.HandsetContract.cellphoneNumber == mobileNumber);
                if (customerContract != null)
                {
                    customerContract.CustomerReference.Load();
                    customerContract.ContractStateReference.Load();
                    customerContract.HandsetContract.CapReference.Load();
                    customerContract.HandsetContract.ExtraDataReference.Load();
                    customerContract.HandsetContract.ExtraMessageReference.Load();
                    customerContract.Customer.OccupationReference.Load();
                    customerContract.HandsetContract.Cap.HandsetBasicInformationReference.Load();
                    customerContract.HandsetContract.Cap.PlansReference.Load();
                }
            }
            catch (Exception ex)
            {
                RecommendationSysBALException exception =
                    new RecommendationSysBALException("Error happens when loading customer information from database", ex);
                throw exception;
            }
            return customerContract;
        }

        public List<CustomerUsageHistroy> GetCustomerUseHistory(string contractID)
        {
            List<CustomerUsageHistroy> histories;
            try
            {
                var result = this.ObjectContext.
                    CustomerUsageHistroySet.Where<CustomerUsageHistroy>(it => it.contractID == contractID);
                histories = result.OrderByDescending(o => o.buildDate).ToList();
            }
            catch (Exception ex)
            {
                RecommendationSysBALException exception =
                    new RecommendationSysBALException("Error happens when loading customer information from database", ex);
                throw exception;
            }
            return histories;
        }

        public List<Customer> GetAllCustomers()
        {
            List<Customer> customers;
            try
            {
                customers = this.ObjectContext.
                    CustomerSet.ToList();
            }
            catch (Exception ex)
            {
                RecommendationSysBALException exception =
                    new RecommendationSysBALException("Error happens when loading all customers from database", ex);
                throw exception;
            }
            return customers;
        }

        public List<V_ContractDetail> GetCustomerContract()
        {
            List<V_ContractDetail> contract;
            try
            {
                DateTime date=Convert.ToDateTime( (DateTime.Today.AddDays(28)));
                var result = this.ObjectContext.V_ContractDetailSet.Where<V_ContractDetail>(it => it.endDate < date).ToList();
                contract = result;
            }
            catch (Exception ex)
            {
                RecommendationSysBALException exception =
                    new RecommendationSysBALException("Error happens when loading customercontract information from database", ex);
                throw exception;
            }
            return contract;
        }
    }
}
